#crud
from sprox.tablebase import TableBase
from sprox.formbase import EditableForm, AddRecordForm
from sprox.fillerbase import TableFiller, EditFormFiller
#turbogears
from tgext.crud import CrudRestController
from tg.controllers import RestController
from tg import expose,require, flash
from tg.controllers import redirect
from tg.decorators import validate

import formencode
from formencode import validators
from repoze.what import predicates
from pylons.i18n import ugettext as _
#model
from pydive.model import DBSession, metadata
from pydive.model.auth import User,Group
from pydive.model.news import News

class NewsController(CrudRestController):
    model = News
    #@expose('tgext.crud.templates.get_all')
    @require(predicates.has_permission('manage', msg=_('Only for managers')))
    @expose('pydive.templates.news.get_all')
    def get_all(self, *args, **kw):
        return super(NewsController, self).get_all(*args, **kw)
    
    @require(predicates.has_permission('news_new', msg=_('Only for managers')))
    @expose('tgext.crud.templates.new')
    def new(self, *args, **kw):
        return super(NewsController, self).new(*args, **kw)
    
    @require(predicates.has_permission('news_new', msg=_('Only for managers')))
    @expose('tgext.crud.templates.new')
    def get(self, *args, **kw):
        return super(NewsController, self).new(*args, **kw)
    
    @require(predicates.has_permission('news_new', msg=_('Only for managers')))
    @expose()
    def post(self, *args, **kw):
        try:
            super(NewsController, self).post(*args, **kw)
        except:
            redirect('/')
    
    @require(predicates.has_permission('news_edit', msg=_('You have no permission to edit news')))
    @expose('tgext.crud.templates.edit')
    def edit(self, *args, **kw):
        return super(NewsController, self).edit(*args, **kw)
    
    @require(predicates.has_permission('news_edit', msg=_('You have no permission to edit news')))
    @expose()
    def put(self, *args, **kw):
        try:
            super(NewsController, self).put(*args, **kw)
        except:
            redirect('/')
    

    class new_form_type(AddRecordForm):
        __model__ = News
        __omit_fields__ = ['id','author_id']
       # __omit_fields__ = ['user_id', 'diver_id','groups','created','_password','diver']
    
    class edit_form_type(EditableForm):
        __model__ = News
        __omit_fields__ = ['id','author_id']
        
    class edit_form_type(EditableForm):
        __model__ = News
        
    class edit_filler_type(EditFormFiller):
        __model__ = News
    
    class table_type(TableBase):
        __model__ = News
    
    class table_filler_type(TableFiller):
        __model__ = News
    
